#ifndef METACENTERSERVER_H
#define METACENTERSERVER_H

#include <QTcpServer>
#include <QObject>
#include <QList>
#include <QString>
#include <QPair>
#include <QSqlDatabase>
#include <QSqlQuery>
#include <set>

#include "peer_node_conn_socket.h"
#include "tcp_meta.h"

class MetaCenterServer : public QTcpServer
{
     Q_OBJECT
public:
    MetaCenterServer(QObject *parent = 0, int port = 0);
    ~MetaCenterServer();

    /// 所有的客户端
    QList<QPair<PeerNodeConnSocket*, QString> > peer_node_socket_list;

    QSqlDatabase db;

    enum MsgKind{
        UpdateName = 0,
        UPDATEMETA = 1,
        UpdateMsg = 2,
        RemoveName = 3,
    };

    static std::string MsgKindStr(enum MsgKind kind)
    {
        switch (kind) {
        case UpdateName:
            return "UpdateName";
        case UPDATEMETA:
            return "UPDATEMETA";
        case UpdateMsg:
            return "UpdateMsg";
        case RemoveName:
            return "RemoveName";
        }
    }

signals:
    void UpdateMetaCenterServer(QString, int, MetaCenterServer::MsgKind);
private slots:
    QTcpSocket* find_socket(QString);

    void UpdatePeerNodes(QString, int);
    void slotDisconnected(int);
    void UpdateUserNameHandle(QString);
    void UpdateUserInfoHandle(QString);
    void UpdateMetaHandle(QString);
    void ReturnMetaHandle(QString);
    void DeleteMeta(QString);
    void SearchMeta(QString);

protected slots:
    void acceptFileConnection();
protected:
    void incomingConnection(int socketDescriptor);
};

#endif // METACENTERSERVER_H
